﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ajayumi.develop.win32.Transport.AppUpgrade.Properties;
using System.Configuration;
using ajayumi.develop.win32.Transport.WCF.Services.DataContracts;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Controls
{
    /// <summary>
    /// 检查更新
    /// Author   : 黄乙冬
    /// Date     : 2011-9-1 08:42:19
    /// </summary>
    public partial class UcCheckUpgrade : UcBase
    {
        private bool m_IsLatestVersion = true;

        public UcCheckUpgrade()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.SetButtonVisible(WizardFormButtons.Exit);
            this.backgroundWorker1.RunWorkerAsync();
        }

        protected override void InitDefaultValue()
        {
            base.InitDefaultValue();

            this.Title = "检查软件更新";
        }

        internal override UcBase Next()
        {
            UcBase uc = null;
            if (this.m_IsLatestVersion)
            {
                uc = new UcFinishLatest(this);
            }
            else
            {
                //if (ArgumentSerialization.IsSilent)
                //{
                //    uc = new UcDownloading(this);
                //}
                //else
                //{
                //    uc = new UcUpgradeDetail(this);
                //}
                uc = new UcUpgradeDetail(this);
            }

            return uc;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (UpgradeServiceClient client = new UpgradeServiceClient())
                {
                    CheckUpgradeVerInfo cuvi = client.CheckVersion(ArgumentSerialization.AppName, ArgumentSerialization.AppVersion);
                    this.m_IsLatestVersion = !cuvi.HasUpgrade;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                e.Cancel = true;
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.PerformNext();
        }
    }
}
